package com.liush.srb.core.controller.admin;

import com.liush.srb.base.result.ResponseEnum;
import com.liush.srb.base.result.ResponseVo;
import com.liush.srb.base.util.SrbAssert;
import com.liush.srb.core.pojo.entity.Lend;
import com.liush.srb.core.pojo.vo.LendInfoVO;
import com.liush.srb.core.service.LendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: liush
 * @CreateTime: 2023-04-10  20:28
 * @Description: 标的准备表 前端控制器
 */
@Api(tags = "admin标的准备模块")
@RestController
@RequestMapping("/admin/core/lend")
public class AdminLendController {

    @Resource
    LendService lendService;

    @ApiOperation("查询标的列表")
    @GetMapping
    public ResponseVo list() {
        List<Lend> lends = lendService.getLends();
        return ResponseVo.ok().data("items", lends);
    }

    @ApiOperation("标的发布")
    @PutMapping("/publish/{id}")
    public ResponseVo publish(@PathVariable("id") Long id) {
        Lend lend = new Lend();
        lend.setId(id);
        lend.setPublishDate(new Date());//发布日期
        lend.setStatus(1);//1募资中
        lend.setCheckTime(new Date());
        lend.setCheckAdminId(1L);//发布标的的管理员id
        boolean b = lendService.updateById(lend);
        SrbAssert.assertTrue(b, ResponseEnum.ERROR);
        return ResponseVo.ok().message("发布成功");
    }

    @ApiOperation("查询标的详情")
    @GetMapping("{id}")
    public ResponseVo getById(@PathVariable("id") Long id) {
        LendInfoVO lendInfoVO = lendService.getLendInfoVOById(id);
        return ResponseVo.ok().data("item", lendInfoVO);
    }

    @ApiOperation("放款")
    @PutMapping("/makeLoan/{id}")
    public ResponseVo makeLoan(@PathVariable("id") Long id) {
        lendService.makeLoan(id);
        return ResponseVo.ok();
    }
}